////////////////////// MMJS.Cart /////////////////////
// Version : 0.1.0
// Require : jQuery 1.8.2
// Author : Albert.Lee
// Last Modify : 2012.10.24
//////////////////////////////////////////////////////////
/* 
base.js 100行 
that.miniCart = new MMJS.Cart();
that.miniCart.bind('.js_cart');
所有方法：
add/remove/clear/get/update
除公共头部会使用到add、remove、get、update方法外
单品页也会用到add和get方法
*/





(function ($) {

/* START Class MMJS.Cart */
window.MMJS = window.MMJS || {};
MMJS.Cart =  function( args ){
  //init args
  args = args || {};
	var currentDomain = document.domain;
	if (currentDomain.split(".").length > 2) {
		currentDomain = currentDomain.substring(currentDomain.indexOf(".") + 1, currentDomain.length)
	}
  var args_default = {
		api_add: 'http://cart.'+currentDomain+'/cart/addToCart',
		api_remove:'http://cart.'+currentDomain+'/cart/removeCartInfoByAjax',
		api_clear:'http://cart.'+currentDomain+'/cart.php?a=clearCartInfoByAjax',
		api_get:'http://cart.'+currentDomain+'/cart/getCartJsonByAjax',
		list:[],
		total_count:0,
		total_price:0,
		$root : null,
		$list : null,
		$item_tpl : null,
		$total_count : null,
		$total_price : null,
		$cart_empty : null,
		$cart_wrap : null
  }
  args = jQuery.extend( args_default,args );
  jQuery.extend( this,args );

}


//绑定购物车html元素
MMJS.Cart.prototype.bind = function( _root ){
	var _this = this;

	_this.$root = jQuery(_root);
	_this.$list = _this.$root.find('.ul-mini-cart-list');
	_this.$item_tpl = _this.$list.children().eq(0).clone(true);

	_this.$total_count = _this.$root.find('.js-cart-num');
	_this.$total_price = _this.$root.find('.s-total-price');

	_this.$cart_empty = _this.$root.find('.not-goods');
	_this.$cart_wrap = _this.$root.find('.has-goods');

	_this.get( function(){
		_this.update();
	} );

}

//添加到购物车
MMJS.Cart.prototype.add = function(data,_callback){
	var _this = this;
	var _data = data || {};
	jQuery.ajax(_this.api_add,{
		data:_data,
		dataType:'jsonp',
		cache : false,
		success:function(data, textStatus, jqXHR){
			if (data){
				_this.get();
				_callback(data);
			}
		}
	});
}


//根据sid删除产品
MMJS.Cart.prototype.remove = function(sid){
	var _this = this;

	jQuery.ajax(_this.api_remove,{
		data:{
			'id':sid
		},
		dataType:'jsonp',
		success:function(data, textStatus, jqXHR){
			if ( data.success ){
				_this.get();
			}
		}
	});
}

//清空购物车
MMJS.Cart.prototype.clear = function(){
	var _this = this;

	jQuery.ajax(_this.api_clear,{
		data:{},
		dataType:'jsonp',
		success:function(data, textStatus, jqXHR){
			if ( data.success ){
				_this.get();
			}
		}
	});

}

//获取最新数据
MMJS.Cart.prototype.get = function( _callback ){
	var _this = this;
	/*
	jQuery.ajax(_this.api_get,{
		data:{},
		dataType:'jsonp',
		success:function(data, textStatus, jqXHR){
			if ( data.success ){
				_this.list = data.data.list || [];
				_this.total_count = data.data.total_count;
				_this.total_price = data.data.total_price;

				_this.update();
			}
		}
	});
	*/
}

//根据当前数据更新购物车
MMJS.Cart.prototype.update = function(){
	var _this = this;

	_this.$total_count.text(_this.total_count);
	_this.$total_price.html('&yen;'+_this.total_price);

	_this.$list.find('li').not('.li-total').remove();

	if ( _this.list.length > 0 ){
		_this.$cart_empty.hide();
		_this.$cart_wrap.show();
	}else{
		_this.$cart_empty.show();
		_this.$cart_wrap.hide();
	}
	var changeNum=0;
	for( var i=0;i<_this.list.length;i++ ){
		var cur_data = _this.list[i];
		var $cur_item = _this.$item_tpl.clone(true);
	
		$cur_item.find('.a-title').text(cur_data.name);
		$cur_item.find('.em-price').html('&yen;'+cur_data.price);
		$cur_item.find('.em-num').text(cur_data.num);
		$cur_item.find('.js_link').attr('href',rrs.itemDomain+'/product/'+cur_data.pid +'.html');
		$cur_item.find('.js_thumb').attr('src',cur_data.thumb);
		if(cur_data.numberChange==1)
		{
			$cur_item.find('.em-num').css("color","red");
			$cur_item.find('.a-title').css("color","red");
			changeNum=1;
		}
		$cur_item.find('.s-del').data('sid',cur_data.sid).bind('click',function(){
			var $this_btn = jQuery(this);
			_this.remove($this_btn.data('sid'));
			return false;
		});
	
		
		_this.$list.prepend($cur_item);
		$cur_item.find('span.hide').removeClass('hide');
		
	}
	if(changeNum==1)
		{
		 	$("#changenumbertips").css("display","block");
		}
	else
		{
			$("#changenumbertips").css("display","none");
		}
	
}
/* END Class MMJS.Cart */


})(jQuery);


$(function(){
	var miniCart = {};
	miniCart = new MMJS.Cart();
	miniCart.bind('.js_cart');
	//miniCart.get();
});
